﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class clsArticulo
    Inherits clsDatos

    Public Articulo As String
    Public ID As Integer
    Public FechaSolicitud As Date
    Public Usuario_Captura As String
    Public Usuario_Autoriza As String
    Public Rama As String
    Public Categoria As String
    Public Grupo As String
    Public Familia As String
    Public Variedad As String
    Public Calidad As String
    Public Origen As String
    Public Pais As String
    Public Marca As String
    Public TipoEmpaque As String
    Public Presentacion As String
    Public Cosecha As String
    Public Grupo2 As String
    Public DescInterna As String
    Public DescFacturacion As String
    Public Linea As String

    Function Art_Guardar() As Integer
        Dim conexion = New SqlConnection(strConexionString)
        Dim comando = New SqlCommand()
        Dim strInsert As String

        strInsert = "INSERT INTO Art_Captura VALUES(GETDATE()," + "N'S'" + ",'" + Usuario_Captura + "',NULL,NULL,'" +
                                                        Rama + "','" +
                                                        Categoria + "','" + Grupo + "','" +
                                                        Familia + "','" + Variedad + "','" +
                                                        Calidad + "','" + Origen + "','" +
                                                        Pais + "','" + Marca + "','" +
                                                        TipoEmpaque + "','" + Presentacion + "','" +
                                                        Cosecha + "','" + Grupo2 + "','" +
                                                        DescInterna + "','" + DescFacturacion + "','" +
                                                        Linea + "')"
        Try
            conexion.Open()
            comando.Connection = conexion
            comando.CommandText = strInsert

            Call comando.ExecuteNonQuery()

            Me.ID = Get_LastID("Select max(capart_id) from art_captura")

            Return Me.ID

        Catch ex As Exception
            Throw ex
        End Try

    End Function

    Function doInsert_into_Intelisis() As Integer
        Dim strInsert As String

        strInsert = "Insert into art ("
        strInsert = strInsert & "articulo, "
        strInsert = strInsert & "descripcion1, "
        strInsert = strInsert & "descripcion2, "
        strInsert = strInsert & "linea, "
        strInsert = strInsert & "rama, "
        strInsert = strInsert & "categoria, "
        strInsert = strInsert & "grupo, "
        strInsert = strInsert & "familia, "
        strInsert = strInsert & "vacavariedad, "
        strInsert = strInsert & "vacacalidad, "
        strInsert = strInsert & "vacaprocedencia, "
        strInsert = strInsert & "pais, "
        strInsert = strInsert & "vacamarca, "
        strInsert = strInsert & "TipoEmpaque, "
        strInsert = strInsert & "Presentacion, "
        strInsert = strInsert & "vacaCosecha, "

        strInsert = strInsert & "Impuesto1, "
        strInsert = strInsert & "TipoOpcion, "
        strInsert = strInsert & "Accesorios, "
        strInsert = strInsert & "Refacciones, "
        strInsert = strInsert & "Sustitutos, "
        strInsert = strInsert & "MonedaCosto, "
        strInsert = strInsert & "MonedaPrecio, "
        strInsert = strInsert & "Estatus, "
        strInsert = strInsert & "Conciliar, "
        strInsert = strInsert & "Refrigeracion, "
        strInsert = strInsert & "TieneCaducidad, "
        strInsert = strInsert & "SeProduce, "
        strInsert = strInsert & "ProdOpciones, "
        strInsert = strInsert & "Tipo, "
        strInsert = strInsert & "VacaLineaAnt, "
        strInsert = strInsert & "VacaFamiliaAnt"
        strInsert = strInsert & ")  values ("

        strInsert = strInsert & "'" & Me.Articulo & "', "
        strInsert = strInsert & "'" & Me.DescInterna & "', "
        strInsert = strInsert & "'" & Me.DescFacturacion & "', "
        strInsert = strInsert & "'" & Me.Linea & "', "
        strInsert = strInsert & "'" & Me.Rama & "', "
        strInsert = strInsert & "'" & Me.Categoria & "', "
        strInsert = strInsert & "'" & Me.Grupo & "', "
        strInsert = strInsert & "'" & Me.Familia & "', "
        strInsert = strInsert & "'" & Me.Variedad & "', "
        strInsert = strInsert & "'" & Me.Calidad & "', "
        strInsert = strInsert & "'" & Me.Origen & "', "
        strInsert = strInsert & "'" & Me.Pais & "', "
        strInsert = strInsert & "'" & Me.Marca & "', "
        strInsert = strInsert & "'" & Me.TipoEmpaque & "', "
        strInsert = strInsert & "'" & Me.Presentacion & "', "
        strInsert = strInsert & "'" & Me.Cosecha & "', "

        strInsert = strInsert & "0, "
        strInsert = strInsert & "'No', "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "'Pesos', "
        strInsert = strInsert & "'Pesos', "
        strInsert = strInsert & "'BLOQUEADO', "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "0, "
        strInsert = strInsert & "'Lote', "
        strInsert = strInsert & "'" & Me.Grupo2 & "',"
        strInsert = strInsert & "'" & Me.Grupo & "'"
        strInsert = strInsert & ")"

        Return Do_ExecuteNonQuery(strInsert)

    End Function

    Function Get_Articulo(ByVal strArticulo As String) As SqlDataReader
        Return GetRegistro("Select * from Art_Captura where capart_id='" & strArticulo & "'")

    End Function

    Function DoBuscaArticulo(ByVal strDescripcion1 As String) As SqlDataReader
        Return GetRegistro("Select * from Art where descripcion1='" & strDescripcion1.Trim & "'")

    End Function

    Function Get_Articulos() As DataTable
        Return GetDatos("Select * from Art_Captura")

    End Function

    ''Function Get_Articulos(ByVal strArticulos As String) As DataTable
    ''    Return GetDatos("Select * from Art_Captura where articulo='" & strArticulos & "'")

    ''End Function

    Function Get_Articulos(ByVal strArticulos As String) As DataTable
        Return GetDatos("Select * from Art_Captura where articulo='" & strArticulos & "'")

    End Function

    Function Get_NextClaveArticulo() As String
        Dim conConexion As New SqlConnection(strConexionString)
        Dim cmdCommando As New SqlCommand("Select max(articulo) from art where tipo='LOTE'", conConexion)
        Dim strUltimaClave As String
        Dim strLetras As String
        Dim strNumeros As String

        conConexion.Open()

        strUltimaClave = cmdCommando.ExecuteScalar

        strLetras = DoFragmentaCadena(strUltimaClave.Trim, 1)
        strNumeros = Integer.Parse(DoFragmentaCadena(strUltimaClave.Trim, 0)) + 1


        Return strLetras & strNumeros

    End Function

    Private Function DoFragmentaCadena(ByVal cadena As String, ByVal opt As Integer) As String

        If cadena = String.Empty Then Return String.Empty

        Dim numeros As String = String.Empty
        Dim letras As String = String.Empty
        Dim simbolos As String = String.Empty

        For Each val As Char In cadena.ToCharArray()
            If Char.IsNumber(val) Then
                numeros += val
            ElseIf Char.IsLetter(val) Then
                letras += val
            ElseIf Char.IsSymbol(val) Or Char.IsPunctuation(val) Then
                simbolos += val
            End If
        Next

        Select Case opt
            Case 0
                Return numeros
            Case 1
                Return letras
            Case 2
                Return simbolos
            Case Else
                Return String.Empty
        End Select

    End Function
End Class
